package cn.itcast.bos.web.action.system;

import cn.itcast.bos.domain.system.Menu;
import cn.itcast.bos.service.system.MenuService;
import cn.itcast.bos.web.action.common.BaseAction;
import com.opensymphony.xwork2.ActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @author: SOS
 * @create: 2018-03-12 09:54
 */
@ParentPackage("json-default")
@Namespace("/")
@Controller
@Scope("prototype")
public class MenuAction extends BaseAction<Menu> {

    @Autowired
    private MenuService menuService;

    /**
     * 查询所有菜单
     * @return
     */
    @Action(value = "menu_list", results = {@Result(name = SUCCESS, type = "json"),
            @Result(name = "input", location = "../../menu.html", type = "redirect")})
    public String menuList() {

        List<Menu> menus = menuService.findAlll();
        ActionContext.getContext().getValueStack().push(menus);
        return SUCCESS;
    }

    /**
     * 获取菜单的父菜单
     * @return
     */
    @Action(value = "menu_listParent", results = {@Result(name = SUCCESS, type = "json"),
            @Result(name = "input", location = "./pages/system/menu.html", type = "redirect")})
    public String menuListParent() {

        List<Menu> menus = menuService.findAlll();
        List<Menu> menuParent = new ArrayList<>();
        for (Menu menu : menus) {
            if (menu.getPage() == null) {
                menuParent.add(menu);
            }
        }
        ActionContext.getContext().getValueStack().push(menuParent);
        return SUCCESS;
    }

    @Action(value = "menu_save", results = {@Result(name = SUCCESS, type = "redirect",location = "./pages/system/menu.html"),
            @Result(name = "input", location = "./pages/system/menu.html", type = "redirect")})
    public String menuSave(){
        menuService.save(model);

        return SUCCESS;
    }
}
